package com.springbooti18n.controll;

import com.springbooti18n.validate.response.I18n;
import com.springbooti18n.config.I18nException;
import com.springbooti18n.model.Home;
import com.springbooti18n.model.User;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * xufan
 * 2019/1/4
 */
@RestController
public class MyInterController {


    @PostMapping("/test")
    public User getMessgae(@RequestBody @Validated User user,
                           @RequestHeader(required = false) String lang){
        user.setAge(120);
        user.setUserName("xxx");
        return user;
    }
    @PostMapping("/testException")
    public Object testException(@RequestHeader(required = false) String lang) throws  I18nException{
        try {
            int i =1/0;
        }catch (Exception e){
            I18nException.Buid().setMessageKey("XXXX");
            System.out.println("抓取到异常");
            throw  I18nException.Buid().setMessageKey("XXXX");
        }
        return  null;
    }


    @PostMapping("/testAddException")
    public Object testAddException(@RequestHeader(required = false) String lang) throws  I18nException{
        try {
            int i =1/0;
        }catch (Exception e){
            List args=new ArrayList();
            args.add("asas");
            System.out.println("抓取到异常");
            throw   I18nException.Buid().setMessageKey("test_add_msg")
                    .setAddMessage(args);
        }
        return  null;
    }




    @PostMapping("/testException12")
    public void testException12(@RequestHeader(required = false) String lang) throws  Exception{

        I18nException e = new I18nException("User.userName");
        e.setMessageKey("User.userName");
        throw  e;
    }


    @PostMapping("/responseBodyAdvice")
    @I18n(baseName="i18n/test",messageKeys = {"moren","next"})
    public Home testResponseBodyAdvice(@RequestHeader(required = false) String lang) throws  Exception{
        Home home = new Home(1,"sasa");
        return  home;
    }

    @PostMapping("/refresh")
    public void refresh() throws  Exception{

    }
}
